<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="请购单编号" prop="reqNo">
        <el-input v-model="queryParams.reqNo" placeholder="请输入请购单编号" clearable />
      </el-form-item>
      <el-form-item label="合同编号" prop="contNo">
        <el-input v-model="queryParams.contNo" placeholder="请输入合同编号" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['etc:purchasereq:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['etc:purchasereq:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['etc:purchasereq:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
          v-hasPermi="['etc:purchasereq:importData']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['etc:purchasereq:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="PurchasereqList" @selection-change="handleSelectionChange"
      :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="合同编号" align="center" prop="contNo" width="150" />

      <el-table-column label="请购单编号" align="center" prop="reqNo" width="180" />

      <el-table-column label="项目进度" align="center" prop="progress" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.progress * 100 }}%</span>
        </template>
      </el-table-column>

      <el-table-column label="需求时间" align="center" prop="demandTime" width="120" />

      <el-table-column label="申请日期" align="center" prop="applyTime" width="120" />

      <el-table-column label="请购类别" align="center" prop="reqType" width="120" show-overflow-tooltip  />

      <el-table-column label="区域/分段" align="center" prop="region" width="120" />

      <el-table-column label="图号" align="center" prop="drawNo" width="120" />

      <el-table-column label="修改单/反馈单" align="center" prop="refurnbillNo" width="120" />

      <el-table-column label="管件号" align="center" prop="pipeNo" width="120" />

      <el-table-column label="物料名称" align="center" prop="mateDesc" width="120" />

      <el-table-column label="物料代码" align="center" prop="mateCode" width="120" />

      <el-table-column label="材质" align="center" prop="mateQuality" width="120" />

      <el-table-column label="壁厚（MM）" align="center" prop="wallThickness" width="120" />

      <el-table-column label="型号" align="center" prop="modelNo" width="120" />

      <el-table-column label="W（MM）" align="center" prop="specsWide" width="120" />

      <el-table-column label="H（MM）" align="center" prop="specsHigh" width="120" />

      <el-table-column label="L（MM）" align="center" prop="specsLong" width="120" />

      <el-table-column label="数量" align="center" prop="num" width="120" />

      <el-table-column label="单位" align="center" prop="numUnit" width="120" />

      <el-table-column label="重量(KG)" align="center" prop="weight" width="120" />

      <el-table-column label="面积(m²)" align="center" prop="area" width="120" />

      <el-table-column label="处理方式" align="center" prop="dealMethod" width="120" />

      <el-table-column label="发货地点" align="center" prop="sendPlace" width="120" />

      <el-table-column label="备注" align="center" prop="remark" width="120" />

      <el-table-column label="创建人" align="center" prop="createName" />

      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="220px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-search" @click="handleView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['etc:purchasereq:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-close" @click="handleDisable(scope.row)"
            v-hasPermi="['etc:purchasereq:disable']" v-if="scope.row.status === 1">禁用</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleDisable(scope.row)"
            v-hasPermi="['etc:purchasereq:disable']" v-else>启用</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['etc:purchasereq:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改材料请购申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="请购单编号" prop="reqNo">
              <el-input v-model="form.reqNo" placeholder="请输入请购单编号" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contNo">
              <el-select v-model="form.contNo" placeholder="请选择合同编号" :disabled="isReadOnly" style="width: 325px;">
                <el-option v-for="item in salecontList" :key="item.contractNo" :label="item.contractNo"
                  :value="item.contractNo"></el-option>
              </el-select>
              <!-- <el-input v-model="form.contNo" placeholder="请输入合同编号" style="width: 325px;" :disabled="isReadOnly" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目进度" prop="progress">
              <el-input type="text" v-model="form.progress" placeholder="请输入项目进度(0-1的小数)"
                oninput="value=value.replace(/^0[0-9]|^[2-9]|^1[0-9]|^1\.|[^\d.]/g,'')" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="6">
            <el-form-item label="项目类别" prop="contNo">
              <el-input v-model="form.contNo" placeholder="请输入项目类别" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col> -->
          <el-col :span="6">
            <el-form-item label="申请日期" prop="applyTime">
              <el-date-picker v-model="form.applyTime" type="date" placeholder="请选择申请日期" format="yyyy-MM-DD"
                value-format="yyyy-MM-DD" style="width: 110px;" :disabled="isReadOnly">
              </el-date-picker>
              <!-- <el-input v-model="form.applyTime" placeholder="请输入申请日期" style="width: 110px;" :disabled="isReadOnly" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="需求日期" prop="demandTime">
              <el-date-picker v-model="form.demandTime" type="date" placeholder="请选择需求日期" format="yyyy-MM-dd"
                value-format="yyyy-MM-dd" style="width: 110px;" :disabled="isReadOnly">
              </el-date-picker>
              <!-- <el-input v-model="form.demandTime" placeholder="请输入需求日期" style="width: 110px;" :disabled="isReadOnly" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请购类别" prop="reqType">
              <el-cascader ref="reqTypeRef" v-model="form.reqType" :options="codeList" :props="{ expandTrigger: 'hover', value: 'name', label: 'name', children: 'childen' }" @change="getCode()" style="width: 325px;"></el-cascader>
              <!-- <el-input v-model="form.reqType" placeholder="请输入请购类别" style="width: 325px;" :disabled="isReadOnly" /> -->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域/分段" prop="region">
              <el-input v-model="form.region" placeholder="请输入区域/分段" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="图号" prop="drawNo">
              <el-input v-model="form.drawNo" placeholder="请输入图号" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="修改单/反馈单" prop="refurnbillNo">
              <el-input v-model="form.refurnbillNo" placeholder="请输入修改单/反馈单" style="width: 325px;"
                :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="管件号" prop="pipeNo">
              <el-input v-model="form.pipeNo" placeholder="请输入管件号" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
            <!-- <el-form-item label="是否黑名单或诉讼记录" prop="isInblacklist">
              <el-select v-model="form.isInblacklist" placeholder="请选择是否有黑名单或诉讼记录" :disabled="isReadOnly">
                <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item> -->
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="mateDesc">
              <!-- <el-select ref="codeRef" v-model="form.mateDesc" placeholder="请选择物料名称" :disabled="isReadOnly"
                @change="getMateCode()">
                <el-option v-for="(dict, index) in codeList" :key="dict.code + index" :label="dict.name"
                  :value="dict.name"></el-option>
              </el-select> -->
              <el-input v-model="form.mateDesc" placeholder="请输入物料名称" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料代码" prop="mateCode">
              <el-cascader ref="mateCodeRef" v-model="form.mateCode" :options="codeList" :props="{ expandTrigger: 'hover', value: 'code', label: 'code', children: 'childen' }" @change="getName()"></el-cascader>
              <!-- <el-input v-model="form.mateCode" placeholder="请输入物料代码" style="width: 110px;" :disabled="isReadOnly" /> -->
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="材质" prop="mateQuality">
              <el-input v-model="form.mateQuality" placeholder="请输入材质" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="壁厚（MM）" prop="wallThickness">
              <el-input v-model="form.wallThickness" placeholder="请输入壁厚" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="modelNo">
              <el-input v-model="form.modelNo" placeholder="请输入型号" style="width: 325px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
            <el-form-item label="W(M)" prop="specsWide">
              <el-input v-model="form.specsWide" placeholder="请输入W(M)" style="width: 60px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="H(M)" prop="specsHigh">
              <el-input v-model="form.specsHigh" placeholder="请输入H(M)" style="width: 60px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="L(M)" prop="specsLong">
              <el-input v-model="form.specsLong" placeholder="请输入L(M)" style="width: 60px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="数量" prop="num">
              <el-input v-model="form.num" placeholder="请输入数量" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单位" prop="numUnit">
              <el-input v-model="form.numUnit" placeholder="请输入单位" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="重量(KG)" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入重量(KG)" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="面积(m²)" prop="area">
              <el-input v-model="form.area" placeholder="请输入面积(m²)" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="处理方式" prop="dealMethod">
              <el-input v-model="form.dealMethod" placeholder="请输入处理方式" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发货地点" prop="sendPlace">
              <el-input v-model="form.sendPlace" placeholder="请输入发货地点" style="width: 110px;" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="描述说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入描述说明" :disabled="isReadOnly" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" action="" :http-request="importData"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的客户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
            @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSalecont } from "@/api/erp/salecont";
import { listPurchasereq, getPurchasereq, delPurchasereq, addPurchasereq, updatePurchasereq, disablePurchasereq, getCodeList } from "@/api/etc/purchasereq";
import { getToken } from "@/utils/auth";
import { downBlobFile } from "@/utils/ruoyi";
import axios from 'axios';

export default {
  name: "Purchasereq",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料请购申请表格数据
      PurchasereqList: [],
      // 合同编号列表
      salecontList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyTime: null,
        area: null,
        contNo: null,
        progress: null,
        dealMethod: null,
        demandTime: null,
        drawNo: null,
        fileUrl: null,
        id: null,
        mateCode: null,
        mateDesc: null,
        mateQuality: null,
        modelNo: null,
        num: null,
        numUnit: null,
        pipeNo: null,
        refurnbillNo: null,
        region: null,
        remark: null,
        reqNo: null,
        reqType: null,
        sendPlace: null,
        specsHigh: null,
        specsLong: null,
        specsWide: null,
        wallThickness: null,
        weight: null
      },
      // 表单参数
      form: {
        applyTime: new Date(),
        area: null,
        contNo: null,
        progress: null,
        dealMethod: null,
        demandTime: null,
        drawNo: null,
        fileUrl: null,
        id: null,
        mateCode: null,
        mateDesc: null,
        mateQuality: null,
        modelNo: null,
        num: null,
        numUnit: null,
        pipeNo: null,
        refurnbillNo: null,
        region: null,
        remark: null,
        reqNo: null,
        reqType: null,
        sendPlace: null,
        specsHigh: null,
        specsLong: null,
        specsWide: null,
        wallThickness: null,
        weight: null
      },
      // 表单校验
      rules: {
        reqNo: [
          { required: true, message: "请购单编号不能为空", trigger: "blur" }
        ],
        contNo: [
          { required: true, message: "合同编号不能为空", trigger: "blur" }
        ],
        progress: [
          { required: true, message: "项目进度不能为空", trigger: "blur" },
        ],
        reqType: [
          { required: true, message: "请购类别不能为空", trigger: "blur" }
        ],
        mateDesc: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        mateCode: [
          { required: true, message: "物料代码不能为空", trigger: "blur" }
        ],
        applyTime: [
          { required: true, message: "申请时间不能为空", trigger: "blur" }
        ],
        demandTime: [
          { required: true, message: "需求时间不能为空", trigger: "blur" }
        ]
      },
      isReadOnly: false,
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的客户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/etc/purchasereq/importData"
      },
      codeList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row.status === 0) {
        return 'warning-row';
      }
      return '';
    },
    /** 查询材料请购申请列表 */
    getList() {
      this.loading = true;
      listPurchasereq(this.queryParams).then(response => {
        this.PurchasereqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询销售合同清单列表 */
    getSalecontList() {
      this.loading = true;
      listSalecont(this.queryParams).then(response => {
        this.salecontList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applyTime: new Date(),
        area: null,
        contNo: null,
        progress: null,
        dealMethod: null,
        demandTime: null,
        drawNo: null,
        fileUrl: null,
        id: null,
        mateCode: null,
        mateDesc: null,
        mateQuality: null,
        modelNo: null,
        num: null,
        numUnit: null,
        pipeNo: null,
        refurnbillNo: null,
        region: null,
        remark: null,
        reqNo: null,
        reqType: null,
        sendPlace: null,
        specsHigh: null,
        specsLong: null,
        specsWide: null,
        wallThickness: null,
        weight: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isReadOnly = false
      this.reset();
      // getPurchasereq(id).then(response => {
      this.open = true;
      this.title = "添加材料请购申请";
      this.getSalecontList();
      this.getCodeList();
      // 默认当月
      var nowDate = new Date();
      var date = {
        year: nowDate.getFullYear(),
        month: nowDate.getMonth() + 1,
        day: nowDate.getDate()
      };
      const dayDate =
        date.year + "-" + (date.month >= 10 ? date.month : "0" + date.month) + "-" + (date.day >= 10 ? date.day : "0" + date.day);
      var date = new Date();
      this.form.applyTime = dayDate.toString();
      console.log(111, dayDate.toString());
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.isReadOnly = true
      this.reset();
      const id = row.id || this.ids
      getPurchasereq(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看材料请购申请";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isReadOnly = false;
      this.reset();
      const id = row.id || this.ids
      getPurchasereq(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改材料请购申请";
      });
    },
    /** 启用禁用按钮操作 */
    handleDisable(row) {
      const id = row.id || this.ids
      let param = {
        id: id.toString()
      }
      disablePurchasereq(param).then(response => {
        console.log('response', response.msg)
        this.$modal.msgSuccess(response.msg);
        this.getList();
      });
    },
    /** 获取物料列表 */
    getCodeList() {
      getCodeList().then(response => {
        let codeListRes = response.rows;
        let codeListCan = []
        codeListRes.forEach((item, index) => {
          // 第一层
          let level1Arr = []
          if (item.level == 0) {
            item.childen = []
            codeListCan.push(item)
          }
          // 第二层
          let level2Arr = []
          codeListCan.forEach((level1Item, n) => {
            if (item.parentId == level1Item.id) {
              level1Item.childen.push(item)
            }
            level1Item.childen.forEach((level2Item, index) => {
              if (!level2Item.childen) {
                level2Item.childen = []
              } else {
                if (item.parentId == level2Item.id) {
                  level2Item.childen.push(item)
                }
              }
            })
          })
        })
        console.log(codeListCan)
        this.codeList = codeListCan;
      });
    },
    getCode () {
      const arr = this.$refs["reqTypeRef"].getCheckedNodes();
      this.form.mateCode = arr[0].data.code;
      this.form.reqType = this.form.reqType[this.form.reqType.length - 1];
      console.log(this.form.mateCode);
    },
    getName () {
      const arr = this.$refs["mateCodeRef"].getCheckedNodes();
      this.form.reqType = arr[0].data.name;
      this.form.mateCode = this.form.mateCode[this.form.mateCode.length - 1];
      console.log(this.form.reqType);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePurchasereq(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchasereq(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除材料请购申请编号为"' + ids + '"的数据项？').then(function () {
        return delPurchasereq(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    importData(file) {
      const loading = this.$loading({
        lock: true,
        text: '数据导入中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      let formData = new FormData();

      formData.append('file', file.file);
      return axios.post(this.upload.url + '?updateSupport=' + this.upload.updateSupport, formData, {
        headers: this.upload.headers,
        responseType: 'blob'
      }).then(res => {
        loading.close()
        this.handleFileSuccess(res, file);
      }).catch(err => {
        loading.close()
      })
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/etc/purchasereq/importTemplate', {
      }, `材料请购申请导入模板_${new Date().getTime()}.xls`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(res, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      if (res.data.type === 'application/json') {
        let reader = new FileReader()
        reader.readAsText(res.data);
        reader.onloadend = () => {
          let rsp = JSON.parse(reader.result);
          if (rsp.code == 200) {
            this.$modal.msgSuccess(rsp.msg);
            this.getList();
          } else {
            this.$modal.msgError(rsp.msg);
          }
        }
      } else {
        this.$modal.msgError("导入错误,请查看回执文件");
        let fileName = file.file.name;
        if (fileName.lastIndexOf(".xls") == fileName.length - 4) {
          fileName = fileName.substring(0, fileName.lastIndexOf(".xls"))
        } else if (fileName.lastIndexOf(".xlsx") == fileName.length - 5) {
          fileName = fileName.substring(0, fileName.lastIndexOf(".xlsx"))
        }
        downBlobFile(downBlobFile(res.data, fileName + "Back.xlsx"));
      }
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.id = this.ids.join(",");
      this.download('etc/purchasereq/export', {
        ...this.queryParams
      }, `材料请购申请_${new Date().getTime()}.xls`)
    },
    /** 获取物料代码 */
    getMateCode() {
      this.codeList.forEach((item, index) => {
        if (item.name == this.form.mateDesc) {
          console.log(item.code)
          this.form.mateCode = item.code
        }
      })
    }
  }
};
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}
</style>
